<template>
	<view class="appointment-detail">
		<view class="content-wrapper">
			<!-- <p style="font-size: 0.9rem; color: #ff9800;">预约就诊时间:
				{{(info.RegistrationTime.substring(0, info.RegistrationTime.length - 8) + ' ' + info.Period + ' ' + info.VisitPeriod)}}
			</p> -->
			<p>就诊人：{{info.PatientName}}</p>
			<p>付款金额：<span style="color: #ff9800;">￥{{info.money}}</span></p>
			<p>挂号状态：{{info.RegistId ? '挂号成功':'挂号失败'}}</p>
			<u-line margin="5px 0px"></u-line>
			<p>就诊开始时间：{{info.reg_start}}</p>
			<p>就诊结束时间：{{info.reg_end}}</p>
			<u-line margin="5px 0px"></u-line>

			<p>订单编号: {{info.ordersn}}</p>
			<p>订单状态: {{info.OrderStatus}}</p>

			<p>预约医生: {{info.Doctor}}</p>
			<p>预约科室: {{info.dept_name}}</p>
			<p>就诊地址: {{info.Address}}</p>

			<p>支付方式: {{info.PaymentMethod}}</p>
			<p>支付时间: {{info.pay_time}}</p>
			<p>支付状态: {{info.PayStatus}}</p>
			<p>就诊状态: {{info.TreatmentState}}</p>

		</view>
		<!-- <view v-if="info.QueueStatus == '1' && info.TreatmentState != '已就诊'" class="content-wrapper">
			<p>注意：执行签到操作后，将无法进行退号退款</p>
		</view> -->
		<u-button v-if="info.OrderStatus == '待签到' && info.RegistId != null" type="success" text="签到"
			@click="signIn()"></u-button>
		<div style="margin-bottom: 5px;"></div>
		<u-button
			v-if="info.RegistId != null && info.TreatmentState == '未就诊' && info.PayStatus == '已支付' && info.OrderStatus != '已退号' && repealBtnVisible"
			type="error" text="退号并退款" @click="repeal()"></u-button>
		<u-button v-if="info.RegistId == null && info.ifpay == '1' && repealBtnVisible" type="error" text="退款"
			@click="onlyMoney()"></u-button>
			
		<p v-if="repealBtnVisible == false && info.OrderStatus != '已退号'" style="margin: 10px 5px 0px 5px;">
			该订单已超过24小时，不能退号退款。
		</p>

		<yangr-msg v-if="yangrMsgShow" :title="yangrMsgTitle" type="error" :info="yangrMsgMessage" btn="确定"
			@yangrMsgEvent="closeYangrMsg"></yangr-msg>
	</view>
</template>

<script>
	import yangrMsg from "@/components/yangr-msg/yangr-msg.vue"
	export default {
		components: {
			yangrMsg
		},
		data() {
			return {
				info: {},
				repealBtnVisible: true,

				yangrMsgTitle: '操作失败',
				yangrMsgMessage: '',
				yangrMsgShow: false
			}
		},
		onLoad(option) {
			const eventChannel = this.getOpenerEventChannel();
			eventChannel.on('detail', (data) => {
				this.info = data.data
				console.log(this.info)
				if (this.isTimeOverOneDay(this.info.reg_start)) {
					this.repealBtnVisible = false
				}
			})
		},
		methods: {
			isTimeOverOneDay(timeString) {
				// 将传入的时间字符串转换为Date对象
				var currentTime = new Date();
				var specifiedTime = new Date(timeString);

				// 计算两个时间之间的分钟差值
				var timeDifferenceInMinutes = Math.abs((currentTime - specifiedTime)) / (1000 * 60);

				// 如果分钟差值大于等于1440（24小时）则返回true，表示超过了24小时；否则返回false
				return timeDifferenceInMinutes >= 1440;
			},
			guahaoStatus(RegistId) {

			},
			signIn() {
				uni.showModal({
					title: '提示',
					content: '您确定已到现场并确认签到排队吗？',
					success: (res) => {
						if (res.confirm) {
							let currentUserCard = uni.getStorageSync('current_user_card')
							this.$app._post_form2("entry/wxapp/apiregistration.guahao_checkin", {
								"cardid": currentUserCard.numcard,
								"regid": this.info.RegistrationId
							}, (res) => {
								console.log(res)
								if (res.status == 1) {
									uni.navigateTo({
										url: '/pages/global/successMessage?msg=签到成功，请注意现场叫号语音'
									})
								} else {
									// uni.showToast({
									// 	title: '签到失败，请联系现场相关工作人员',
									// 	icon: 'none',
									// 	duration: 3000
									// })
									this.yangrMsgShow = true
									this.yangrMsgMessage = '签到失败，请联系现场相关工作人员'
								}
							})
						} else if (res.cancel) {

						}
					}
				});
			},
			repeal() {
				uni.showModal({
					title: '提示',
					content: '确定要取消挂号并退款吗？',
					success: (res) => {
						if (res.confirm) {
							this.api_apiregistration_cancel()
						} else if (res.cancel) {

						}
					}
				});
			},
			onlyMoney() {
				uni.showModal({
					title: '提示',
					content: '确定要退款吗？',
					success: (res) => {
						if (res.confirm) {
							let openid = uni.getStorageSync('openid')
							this.$app._post_form2("entry/wxapp/apiregistration.guahao_refundbyid", {
								openid: openid,
								id: this.info.id
							}, (res) => {
								console.log(res)
								if (res.status == 1) {
									uni.navigateTo({
										url: '/pages/global/successMessage?msg=' + res.data
									})
								} else {
									this.yangrMsgShow = true
									this.yangrMsgMessage = res.msg
								}
							})
						} else if (res.cancel) {

						}
					}
				});
			},
			api_apiregistration_cancel() {
				let currentUserCard = uni.getStorageSync('current_user_card')
				let openid = uni.getStorageSync('openid')
				this.$app._post_form2("entry/wxapp/apiregistration.guahao_cancel", {
					openid: openid,
					form: {
						"CardID": currentUserCard.numcard,
						"RegistId": this.info.RegistrationId,
						"HisPaymentMethodEnum": "14",
						"Money": this.info.Amount,
						"Reason": "患者取消预约"
					}
				}, (res) => {
					console.log(res)
					if (res.status == 1) {
						uni.navigateTo({
							url: '/pages/global/successMessage?msg=' + res.data
						})
					} else if (res.status == 0) {
						this.yangrMsgShow = true
						this.yangrMsgMessage = res.msg
					}
				})
			},
			closeYangrMsg() {
				this.yangrMsgShow = false
			}
		}
	}
</script>

<style lang="scss" scoped>
	.appointment-detail {
		width: 100%;
		height: 100%;
		position: relative;
		color: #757575;
		padding: 15px;
		box-sizing: border-box;

		.content-wrapper {
			border-radius: 6px;
			border: 1px solid #e3e3e3;
			padding: 10px;
			line-height: 30px;
			background-color: #f5f5f5;
			font-size: .8rem;
			color: #757575;
			margin-bottom: 10px;

			p {
				display: block;
			}
		}


	}
</style>